package com.sicau.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.sicau.common.utils.DateUtils;
import com.sicau.common.utils.StringUtils;
import com.sicau.system.domain.BMessageUser;
import com.sicau.system.mapper.BMessageUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sicau.system.mapper.BMessageMapper;
import com.sicau.system.domain.BMessage;
import com.sicau.system.service.IBMessageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 消息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-19
 */
@Service
public class BMessageServiceImpl implements IBMessageService 
{
    @Autowired
    private BMessageMapper bMessageMapper;

    @Autowired
    private BMessageUserMapper bMessageUserMapper;

    /**
     * 查询消息
     * 
     * @param id 消息主键
     * @return 消息
     */
    @Override
    public BMessage selectBMessageById(Long id)
    {
        return bMessageMapper.selectBMessageById(id);
    }

    /**
     * 查询消息列表
     * 
     * @param bMessage 消息
     * @return 消息
     */
    @Override
    public List<BMessage> selectBMessageList(BMessage bMessage)
    {
        return bMessageMapper.selectBMessageList(bMessage);
    }

    /**
     * 新增消息
     * 
     * @param bMessage 消息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBMessage(BMessage bMessage,Long[] userIds)
    {
        bMessage.setCreateTime(DateUtils.getNowDate());
        int result = bMessageMapper.insertBMessage(bMessage);
        insertBMessageUsers(bMessage.getId(),userIds);
        return result;
    }

    /**
     * 修改消息
     * 
     * @param bMessage 消息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBMessage(BMessage bMessage)
    {
        bMessage.setUpdateTime(DateUtils.getNowDate());
        bMessageUserMapper.deleteBMessageUserByMessageId(bMessage.getId());
        insertBMessageUsers(bMessage.getId(), bMessage.getUserIds());
        return bMessageMapper.updateBMessage(bMessage);
    }

    /**
     * 批量删除消息
     * 
     * @param ids 需要删除的消息主键
     * @return 结果
     */
    @Override
    public int deleteBMessageByIds(Long[] ids)
    {
        return bMessageMapper.deleteBMessageByIds(ids);
    }

    /**
     * 删除消息信息
     * 
     * @param id 消息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBMessageById(Long id)
    {
        bMessageUserMapper.deleteBMessageUserByMessageId(id);
        return bMessageMapper.deleteBMessageById(id);
    }



    public int insertBMessageUsers(Long messageId, Long[] userIds) {
        if (StringUtils.isNotEmpty(userIds)) {
            List<BMessageUser> list = new ArrayList<>();
            for (Long userId : userIds) {
                BMessageUser bMessageUser = new BMessageUser();
                bMessageUser.setMessageId(messageId);
                bMessageUser.setUserId(userId);
                list.add(bMessageUser);
            }
            return bMessageUserMapper.insertBMessageUsers(list);
        }
        return 0;
    }
}
